package com.yungu.swift.api.route.v2.passenger;

import com.alibaba.dubbo.config.annotation.Reference;
import com.github.miemiedev.mybatis.paginator.domain.PageList;
import com.yungu.swift.api.base.BaseApi;
import com.yungu.swift.api.base.gateway.CurrentUser;
import com.yungu.swift.assets.passenger.model.dto.PassengerInvoiceDto;
import com.yungu.swift.assets.passenger.model.dto.PassengerRechargeDto;
import com.yungu.swift.assets.passenger.model.param.ApiPassengerInvoiceParam;
import com.yungu.swift.assets.passenger.service.PassengerInvoiceService;
import com.yungu.swift.assets.passenger.service.PassengerWalletService;
import com.yungu.swift.base.ResponseData;
import com.yungu.swift.base.model.api.ApiAdCodeParam;
import com.yungu.swift.base.model.api.ApiPageParam;
import com.yungu.swift.base.model.api.ApiParam;
import com.yungu.swift.constants.MagicConstant;
import com.yungu.swift.order.model.vo.JsonOrderListVo;
import com.yungu.swift.order.service.OrderService;
import com.yungu.swift.system.finance.model.dto.FinanceActualFlowDto;
import com.yungu.swift.system.finance.service.FinanceActualFlowService;
import com.yungu.swift.system.sys.model.vo.SysPassengerAccountFlowVo;
import com.yungu.swift.system.sys.service.SysAccountPassengerService;
import com.yungu.swift.utils.MapUtils;
import com.yungu.swift.utils.StringUtils;
import com.yungu.swift.utils.ValidateUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static java.util.regex.Pattern.compile;

/**
 * Created by cuixiuyin on 2019/11/20.
 * 乘客钱包API接口
 */
@RestController
@RequestMapping(value = "/api/v2/passenger/token/wallet")
@Api(value = "【乘客】【钱包】乘客钱包接口V2", tags = "V2-乘客钱包接口")
public class PasWalletApi extends BaseApi {
    @Reference
    private PassengerWalletService passengerWalletService;
    @Reference
    private OrderService orderService;
    @Reference
    private FinanceActualFlowService financeActualFlowService;
    @Reference
    private PassengerInvoiceService passengerInvoiceService;
    @Reference
    private SysAccountPassengerService sysAccountPassengerService;


    @ApiOperation(value = "返回乘客钱包信息")
    @PostMapping(value = {"/getWallet"})
    public ResponseData<Map<String, Object>> getWallet(@CurrentUser ApiParam apiParam) {
        return passengerWalletService.getWallet(apiParam.getUserUuid());
    }

    @ApiOperation(value = "乘客账户明细", notes = "分页流水信息")
    @PostMapping(value = {"/account"})
    public ResponseData<PageList<SysPassengerAccountFlowVo>> account(@RequestBody ApiPageParam apiPageParam) {
        return sysAccountPassengerService.flowQueryPage(apiPageParam);
    }

    @ApiOperation(value = "返回乘客充值面额")
    @PostMapping(value = {"/listDenominations"})
    public ResponseData<Collection<PassengerRechargeDto>> listDenominations(@RequestBody ApiAdCodeParam adCodeParam) {
        return passengerWalletService.listDenominations(adCodeParam.getAdCode());
    }

    @ApiOperation(value = "按照行程开票", notes = "返回成功或失败")
    @PostMapping(value = {"/billingByRoute"})
    public ResponseData<List<JsonOrderListVo>> billingByRoute(@RequestBody ApiPageParam apiPageParam) {
        return orderService.queryBillOrder(apiPageParam);
    }

    @ApiOperation(value = "按照充值金额开票", notes = "返回成功或失败")
    @PostMapping(value = {"/billingByCharge"})
    public ResponseData<List<FinanceActualFlowDto>> billingByCharge(@RequestBody ApiPageParam apiPageParam) {
        return financeActualFlowService.queryBillFlow(apiPageParam);
    }

    @ApiOperation(value = "开票", notes = "返回成功或失败")
    @PostMapping(value = {"/billing"})
    public ResponseData<PassengerInvoiceDto> billing(@Valid @RequestBody ApiPassengerInvoiceParam pasInvoiceParam) {
        if (!StringUtils.isEmpty(pasInvoiceParam.getMobile()) && !ValidateUtils.validateMobile(pasInvoiceParam.getMobile())) {
            return ResponseData.buildErrorResponse(ResponseData.ERROR_CODE, "手机号码格式错误");
        }
        if (!StringUtils.isEmpty(pasInvoiceParam.getTaxpayerNo())) {
            if (pasInvoiceParam.getTaxpayerNo().length() < MagicConstant.INT_FIFTEEN || pasInvoiceParam.getTaxpayerNo().length() > 20) {
                return ResponseData.buildErrorResponse(ResponseData.ERROR_CODE, "纳税人识别码格式错误");
            }
            Pattern pattern = compile("[\\W]");
            Matcher matcher = pattern.matcher(pasInvoiceParam.getTaxpayerNo());
            if (matcher.find()) {
                return ResponseData.buildErrorResponse(ResponseData.ERROR_CODE, "纳税人识别码格式错误");
            }
        }
        return passengerInvoiceService.billing(pasInvoiceParam);
    }

    @ApiOperation(value = "开票历史", notes = "返回成功或失败")
    @PostMapping(value = {"/listBillingHistory"})
    public ResponseData<PageList<PassengerInvoiceDto>> listBillingHistory(@RequestBody Map<String, Integer> typeMap,
                                                                          @CurrentUser ApiParam apiParam) {
        Map<String, Object> map = MapUtils.build(4);
        map.put("passengerUuid", apiParam.getUserUuid());
        // 发票类型
        map.put("type", typeMap.get("type"));
        return passengerInvoiceService.queryPage(map, new ApiPageParam(typeMap.get("nowPage")));
    }
}
